package com.yjt.fresh.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiUpdatePassword;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToast;
import com.yjt.fresh.widget.CustomToastResult;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by UPC on 2018/8/20.
 */

public class AtyNewPassword extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.tvAtySetPasswordTitle)
    TextView mTvTitle;
    @BindView(R.id.edtAPPAtySetPasswordPassword)
    EditText mEdtPassword;
    @BindView(R.id.ivAPPAtySetPasswordSwitchPassword)
    ImageView mIvSwitchPassword;
    @BindView(R.id.edtAPPAtySetPasswordConfirmPassword)
    EditText mEdtConfirmPassword;
    @BindView(R.id.ivAPPAtySetPasswordSwitchConfirmPassword)
    ImageView mIvSwitchConfirmPassword;
    @BindView(R.id.btnAPPAtySetPasswordComplete)
    Button mBtnComplete;
    private boolean isPutConfirmPassword;
    private boolean isPutPassword;
    private boolean isConfirmPasswordOpen;
    private boolean isPasswordOpen;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_set_password;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mTvTitle.setText("新密码");
    }

    @Override
    protected void initEvent() {
        mEdtConfirmPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count >0){
                    isPutConfirmPassword = true;
                }else {
                    isPutConfirmPassword = false;
                }
                if (isPutPassword){
                    mBtnComplete.setClickable(true);
                    mBtnComplete.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnComplete.setClickable(false);
                    mBtnComplete.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mEdtPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0){
                    isPutPassword = true;
                }else {
                    isPutPassword = false;
                }

                if (isPutConfirmPassword){
                    mBtnComplete.setClickable(true);
                    mBtnComplete.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnComplete.setClickable(false);
                    mBtnComplete.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mIvSwitchConfirmPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isConfirmPasswordOpen){
                    mIvSwitchConfirmPassword.setImageResource(R.mipmap.a_ic_eye_2);
                    mEdtConfirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    mIvSwitchConfirmPassword.setImageResource(R.mipmap.a_ic_eye);
                    mEdtConfirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                isConfirmPasswordOpen = !isConfirmPasswordOpen;
            }
        });

        mIvSwitchPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isPasswordOpen){
                    mIvSwitchPassword.setImageResource(R.mipmap.a_ic_eye_2);
                    mEdtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    mIvSwitchPassword.setImageResource(R.mipmap.a_ic_eye);
                    mEdtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                isPasswordOpen = !isPasswordOpen;
            }
        });

        mBtnComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = mEdtPassword.getText().toString();
                if (!password.equals(mEdtConfirmPassword.getText().toString())){
                    CustomToast.show("两次输入的密码不一致");
                    return;
                }
                if (password.length() < 6 || password.length() > 16){
                    CustomToast.showLong("密码在6-16位字符之间");
                }
                if (!CommonsToolsHelper.isContainNumber(password)){
                    CustomToast.showLong("密码必须包含数字");
                    return;
                }

                if (!CommonsToolsHelper.isContainsLetter(password)){
                    CustomToast.showLong("密码必须包含字母");
                    return;
                }

                if (CommonsToolsHelper.isContainsSpecialChar(password)){
                    CustomToast.showLong("密码不能包含非法字符");
                    return;
                }

                setNewPassword();
            }
        });
    }

    private void setNewPassword() {
        ApiUpdatePassword apiUpdatePassword = new ApiUpdatePassword(new HttpOnNextListener<BaseResultEntity>() {


            @Override
            public void onNext(BaseResultEntity baseResultEntity) {
                CustomToastResult.show("修改成功");
                toLogin();
                finish();
            }

            @Override
            public void onReLogin() {

            }

            @Override
            public void failed(String massage) {
                super.failed(massage);
                Toast.makeText(mContext, massage, Toast.LENGTH_SHORT).show();
            }
        },mContext,true,false,true);
        Map<String,Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("OLD_LOGIN_PASSWORD",getIntent().getStringExtra(MyConstants.PASSWORD));
        map.put("NEW_LOGIN_PASSWORD",mEdtConfirmPassword.getText().toString().trim());
        apiUpdatePassword.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiUpdatePassword);
    }


}
